package com.easylive.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.po.UserVideoSeriesVideo;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.UserVideoSeriesVideoQuery;
import com.easylive.entity.query.VideoInfoQuery;
import com.easylive.exception.BusinessException;
import com.easylive.mappers.UserVideoSeriesVideoMapper;
import com.easylive.mappers.VideoInfoMapper;
import org.springframework.stereotype.Service;

import com.easylive.entity.enums.PageSize;
import com.easylive.entity.query.UserVideoSeriesQuery;
import com.easylive.entity.po.UserVideoSeries;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.query.SimplePage;
import com.easylive.mappers.UserVideoSeriesMapper;
import com.easylive.service.UserVideoSeriesService;
import com.easylive.utils.StringTools;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户视频列表 业务接口实现
 */
@Service("userVideoSeriesService")
public class UserVideoSeriesServiceImpl implements UserVideoSeriesService {

	@Resource
	private UserVideoSeriesMapper<UserVideoSeries, UserVideoSeriesQuery> userVideoSeriesMapper;
	@Resource
	private UserVideoSeriesVideoMapper<UserVideoSeriesVideo, UserVideoSeriesVideoQuery> userVideoSeriesVideoMapper;
	@Resource
	private VideoInfoMapper<VideoInfo, VideoInfoQuery> videoInfoMapper;


	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<UserVideoSeries> findListByParam(UserVideoSeriesQuery param) {
		return this.userVideoSeriesMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(UserVideoSeriesQuery param) {
		return this.userVideoSeriesMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<UserVideoSeries> findListByPage(UserVideoSeriesQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<UserVideoSeries> list = this.findListByParam(param);
		PaginationResultVO<UserVideoSeries> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(UserVideoSeries bean) {
		return this.userVideoSeriesMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<UserVideoSeries> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.userVideoSeriesMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<UserVideoSeries> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.userVideoSeriesMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(UserVideoSeries bean, UserVideoSeriesQuery param) {
		StringTools.checkParam(param);
		return this.userVideoSeriesMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(UserVideoSeriesQuery param) {
		StringTools.checkParam(param);
		return this.userVideoSeriesMapper.deleteByParam(param);
	}

	/**
	 * 根据SeriesId获取对象
	 */
	@Override
	public UserVideoSeries getUserVideoSeriesBySeriesId(Integer seriesId) {
		return this.userVideoSeriesMapper.selectBySeriesId(seriesId);
	}

	/**
	 * 根据SeriesId修改
	 */
	@Override
	public Integer updateUserVideoSeriesBySeriesId(UserVideoSeries bean, Integer seriesId) {
		return this.userVideoSeriesMapper.updateBySeriesId(bean, seriesId);
	}

	/**
	 * 根据SeriesId删除
	 */
	@Override
	public Integer deleteUserVideoSeriesBySeriesId(Integer seriesId) {
		return this.userVideoSeriesMapper.deleteBySeriesId(seriesId);
	}

	@Override
	public List<UserVideoSeries> getUserAllSeries(String userId) {
		return userVideoSeriesMapper.selectUserAllSeries(userId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveUserVideoSeries(UserVideoSeries userVideoSeries, String videoIds) {
		if(userVideoSeries.getSeriesId() == null && StringTools.isEmpty(videoIds)){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		if(userVideoSeries.getSeriesId() == null){
			checkVideoIds(videoIds,userVideoSeries.getUserId());
			userVideoSeries.setSort(userVideoSeriesMapper.selectMaxSort(userVideoSeries.getUserId()) + 1);
			userVideoSeries.setUpdateTime(new Date());
			userVideoSeriesMapper.insert(userVideoSeries);
			this.saveSeriesVideo(userVideoSeries.getUserId(),videoIds,userVideoSeries.getSeriesId());
		}else{
			UserVideoSeriesQuery query =  new UserVideoSeriesQuery();
			query.setSeriesId(userVideoSeries.getSeriesId());
			query.setUserId(userVideoSeries.getUserId());
			userVideoSeriesMapper.updateByParam(userVideoSeries,query);


		}
	}




	@Override
	public void saveSeriesVideo(String userId, String videoIds, Integer seriesId) {
		UserVideoSeries userVideoSeries = userVideoSeriesMapper.selectBySeriesId(seriesId);
		if(userVideoSeries == null || !userId.equals(userVideoSeries.getUserId())){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		checkVideoIds(videoIds,userId);
		String[] videoIdArray = videoIds.split(",");
		Integer maxSort = userVideoSeriesVideoMapper.selectMaxSort(seriesId);
		List<UserVideoSeriesVideo> userVideoSeriesVideoList = new ArrayList<>();
		for(String videoId : videoIdArray){
			UserVideoSeriesVideo userVideoSeriesVideo = new UserVideoSeriesVideo();
			userVideoSeriesVideo.setSeriesId(seriesId);
			userVideoSeriesVideo.setVideoId(videoId);
			userVideoSeriesVideo.setSort(++maxSort);
			userVideoSeriesVideo.setUserId(userId);
			userVideoSeriesVideoList.add(userVideoSeriesVideo);
		}
		userVideoSeriesVideoMapper.insertOrUpdateBatch(userVideoSeriesVideoList);


	}

	@Override
	public void delSeriesVideo(String userId, String videoId, Integer seriesId) {
		UserVideoSeriesVideoQuery param = new UserVideoSeriesVideoQuery();
		param.setSeriesId(seriesId);
		param.setVideoId(videoId);
		param.setUserId(userId);
		userVideoSeriesVideoMapper.deleteByParam(param);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delVideoSeries(String userId, Integer seriesId) {
		UserVideoSeriesQuery  query = new UserVideoSeriesQuery();
		query.setSeriesId(seriesId);
		query.setUserId(userId);
		Integer count = userVideoSeriesMapper.deleteByParam(query);
		if(count == 0){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		UserVideoSeriesVideoQuery param = new UserVideoSeriesVideoQuery();
		param.setSeriesId(seriesId);
		param.setUserId(userId);
		userVideoSeriesVideoMapper.deleteByParam(param);
	}

	@Override
	public void changeVideoSeriesSort(String userId, String seriesIds) {
		String[] seriesIdArray = seriesIds.split(",");
		List<UserVideoSeries> userVideoSeriesList = new ArrayList<>();
		Integer sort = 0;
		for(String seriesId : seriesIdArray){
			UserVideoSeries userVideoSeries = new UserVideoSeries();
			userVideoSeries.setSeriesId(Integer.valueOf(seriesId));
			userVideoSeries.setUserId(userId);
			userVideoSeries.setSort(++sort);
			userVideoSeriesList.add(userVideoSeries);
		}
		userVideoSeriesMapper.changeSort(userVideoSeriesList);

	}

	@Override
	public List<UserVideoSeries> findListWithVideoByParam(UserVideoSeriesQuery param) {
		return userVideoSeriesMapper.selectListWithVideo(param);
	}

	private void checkVideoIds(String videoIds,String userId){
		String[] videoIdArray = videoIds.split(",");
		VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
		videoInfoQuery.setVideoIdArray(videoIdArray);
		videoInfoQuery.setUserId(userId);
		int count = videoInfoMapper.selectCount(videoInfoQuery);
		if(count != videoIdArray.length){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
	}
}